/*
 *  *****************************************************************************
 *  * Copyright (C) 2022 szjzsoft Co.,Ltd
 *  * All Rights Reserved.
 *  * 本软件为深圳市金政软件技术有限公司（平台）研制。未经本公司正式书面同意，其他任何个人、团体
 *  * 不得使用、复制、修改或发布本软件.
 *  ****************************************************************************
 */

package cc.bookcloud.tool.ocr.intercept;

import com.alibaba.fastjson.JSON;
import cc.bookcloud.tool.ocr.common.dirs.ReqParamConstant;
import cc.bookcloud.tool.ocr.util.IPUtil;
import cc.bookcloud.tool.ocr.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLDecoder;
import java.util.Map;

/**
 * @Desc: 目录服务拦截处理
 * @version: 2.0
 * @author: John Goo
 * @date: 2022/04/27 14:37
 **/
@Slf4j
@Component
public class FSInterceptor implements HandlerInterceptor {


    @Resource
    private Environment environment;


//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        //调用转换SQL脚本接口，清理ThreadLocal中的数据
//        String matchURI = request.getRequestURI();
//        if ("/release/dbscript/convert".equals(matchURI)) {
//            ThreadLocal<List> tl = TableConvertDbScriptEngine.getTl();
//            tl.remove();
//        }
//    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String matchURI = request.getRequestURI();
        parseRequestInfo(request, matchURI);
        matchURI = URLDecoder.decode(matchURI, "UTF-8");
        // 非文件目录服务,放行
        if (!matchURI.startsWith(ReqParamConstant.DS_FIXED_URI)) {
            return true;
        }
        String childDir = matchURI.replace(ReqParamConstant.DS_FIXED_URI, "");
        String rootStoreDir = environment.getProperty("fs.dir");
        if (StringUtil.isEmpty(rootStoreDir)) {
            // 默认路径
            rootStoreDir = "/SZJZ";
        }

        // 文件存储路径
        String fsPath = rootStoreDir + "/" + childDir;
        File file = new File(fsPath);
        // 目录，继续遍历
        if (file.isDirectory()) {
            // 子目录
            request.getSession().setAttribute(ReqParamConstant.REQ_URI, matchURI);
            request.getSession().setAttribute(ReqParamConstant.REQ_FS_PATH, fsPath);
            // 转向目录服务
            request.getRequestDispatcher("/release/fs/go").forward(request, response);
            return false;
        }
        return true;
    }

    public void parseRequestInfo(HttpServletRequest request, String uri) {
        String ip = IPUtil.parseIpAddress(request);
        Map params = IPUtil.parseRequestParams(request);
        String addrInfo = uri + " [" + ip + "]";
        log.info("==请求:" + addrInfo + " ==> 参数：" + JSON.toJSONString(params));

    }
}
